﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using Micua.Core.Configuration;
using Micua.Model;
using Micua.Model.Struct;
using Micua.Utility;

namespace Micua.Extension
{
    /// <summary>
    /// 枚举器拓展方法类
    /// </summary>
    public static class Enumerable
    {
        #region 生成标签枚举器链接集合 +static MvcHtmlString ToHtml(this IEnumerable<Category> source, UrlHelper url, string format, string separator = "、")
        /// <summary>
        /// 生成标签枚举器链接集合
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="url">UrlHelper</param>
        /// <param name="format">
        /// 格式({0}:链接；{1}:名称)
        /// eg:"<a href="{0}" target="_blank" class="label label-important">{1}<i class="label-arrow"></i></a>"
        /// </param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static MvcHtmlString ToHtml(this IEnumerable<Category> source, UrlHelper url, string format, string separator = "、")
        {
            var categories = source as Category[] ?? source.ToArray();
            if (categories.Length == 0)
                return MvcHtmlString.Create(string.Format(format, "#", "Uncategorized".GetCultureString(Setting.GetString(OptionKeys.SiteCulture))));
            string html = !categories.Any()
                ? string.Empty
                : string.Join(separator, categories.Select(c => string.Format(format, url.Category(string.IsNullOrEmpty(c.Slug) ? c.Name : c.Slug), c.Name)));
            return MvcHtmlString.Create(html);
        }
        #endregion

        #region 生成标签枚举器链接集合 +static MvcHtmlString ToHtml(this IEnumerable<Category> source, UrlHelper url, string format, string separator = "、")
        /// <summary>
        /// 生成标签枚举器链接集合
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="url">UrlHelper</param>
        /// <param name="format">
        /// 格式({0}:链接；{1}:名称)
        /// eg:"<a href="{0}\" target="_blank" class="label label-important">{1}<i class="label-arrow"></i></a>"
        /// </param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static MvcHtmlString ToHtml(this IEnumerable<Tag> source, UrlHelper url, string format, string separator = "、")
        {
            var tags = source as Tag[] ?? source.ToArray();
            if (tags.Length == 0)
                return MvcHtmlString.Create(string.Format(format, "#", "Unlabeled".GetCultureString(Setting.GetString(OptionKeys.SiteCulture))));
            string html = !tags.Any()
                ? string.Empty
                : string.Join(separator, tags.Select(c => string.Format(format, url.Category(string.IsNullOrEmpty(c.Slug) ? c.Name : c.Slug), c.Name)));
            return MvcHtmlString.Create(html);
        }
        #endregion

        #region 处理重复别名业务 +static string GetSlug(this IEnumerable<string> source, string slug)
        /// <summary>
        /// 处理重复别名业务
        /// 暨如果该别名存在的情况下,返回slug-1
        /// </summary>
        /// <remarks>
        ///  2013-12-22 20:20:25 Created By iceStone
        /// </remarks>
        /// <param name="source">已经存在的别名集合</param>
        /// <param name="slug">新的别名</param>
        /// <example>
        /// {"test","test-1"}
        /// test=>test-2
        /// </example>
        /// <returns></returns>
        public static string GetSlug(this IEnumerable<string> source, string slug)
        {
            slug = slug.ToSlug();
            var exist = source as string[] ?? source.ToArray();
            if (exist.All(e => e != slug)) return slug;
            //存在该Slug;
            var last = exist.OrderByDescending(s => s).FirstOrDefault();
            if (last == null) return slug;
            var regex = new Regex(@"^.+\-(\d+)$");
            if (regex.IsMatch(last))
            {
                var match = regex.Match(last);
                var max = match.Groups[1].Value.ToInt32() + 1;
                slug = slug + "-" + max;
            }
            else
            {
                slug = slug + "-1";
            }
            return slug;
        }
        #endregion
    }
}
